package com.dianxin.farm.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dianxin.farm.entity.Manager;
import com.dianxin.farm.filter.AuthorCheck;
import com.dianxin.farm.filter.LoginInterceptor;
import com.dianxin.farm.request.account.AccountRequest;
import com.dianxin.farm.service.CurrentLoginService;
import com.dianxin.farm.service.IManagerService;
import com.dianxin.farm.util.exception.UserOperationException;
import com.dianxin.farm.util.util.UNIDGenerate;
import com.dianxin.farm.util.web.ApiResponse;
import com.dianxin.farm.util.web.ApiResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * <p>
 * 管理员账号控制器控制器
 * </p>
 *
 * @author xukai
 * @since 2020-03-17
 */
@RestController
@RequestMapping(value = "/account", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
@Api(description = "账户信息控制")
@AuthorCheck
public class AccountController {

    private String password = "123456";

    @Resource
    private IManagerService iManagerService;
    @Resource
    private CurrentLoginService currentLoginService;
    @Resource
    private HttpSession httpSession;

    @RequestMapping(value = "/login")
    @ResponseBody
    @AuthorCheck(authorCheck = false)
    public ApiResponse<Manager> login(@Valid @RequestBody AccountRequest request) {
        String accountName = request.getAccountName();
        String password = request.getAccountPassword();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("account_name", accountName);
        wrapper.eq("account_password", password);
        Manager manager = iManagerService.getOne(wrapper);
        if (manager == null) {
            throw new UserOperationException("账号不存在或者密码错误");
        }
        //manager.setToken(new UNIDGenerate().getUnid());
        iManagerService.updateById(manager);
        manager.setAccountPassword("******");
        return ApiResponseUtils.success(manager);
    }


    @RequestMapping(value = "/edit")
    @ResponseBody
    @AuthorCheck
    @ApiOperation("重置密码")
    public ApiResponse<Manager> edit(@Valid @RequestBody AccountRequest request) {
        Manager manager = currentLoginService.getManager();
        String password = request.getAccountPassword();
        manager.setAccountPassword(password);
        QueryWrapper wrapper = new QueryWrapper();
        iManagerService.updateById(manager);
        return ApiResponseUtils.success(manager);
    }


    @RequestMapping(value = "/addSonManager")
    @ResponseBody
    @AuthorCheck
    @ApiOperation("新增子账号,没有的字段请传空。密码如果未设置，将默认密码123456")
    public ApiResponse<Manager> addSonManager(@Valid @RequestBody Manager request) {
        String password=request.getAccountPassword();
        if (password==null || "".equals(password)){
            password="123456";
        }
        request.setAccountPassword(password);
        request.setId(0);
        request.setCreateTime(LocalDateTime.now());
        iManagerService.save(request);
        return ApiResponseUtils.success();
    }


    @ApiOperation("退出")
    @RequestMapping(value = "/logout")
    @ResponseBody
    @AuthorCheck
    public ApiResponse logout() {
        Manager manager = currentLoginService.getManager();
        manager.setToken(new UNIDGenerate().getUnid());
        iManagerService.updateById(manager);
        httpSession.removeAttribute(LoginInterceptor.CERTIFICATE_SESSION_KEY);
        return ApiResponseUtils.success();
    }


}
